<?php
include_once ("../Login/databaseconnection.php");
$Pro_Name=$_POST['Pro_Name'];
$Grade=$_POST['Grade'];
$resultp= mysql_query("SELECT * FROM programme Where Programme_Name='$Pro_Name'");
if($rowp = mysql_fetch_array($resultp))
{
 $P_Id=$rowp['Programme_Id'];
}

$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $S_Id=$row['School_Id'];
}
   $Year=$_POST['Year'];

if($Pro_Name!="Select One"&&$SchName!=""&&$Year!="Select One"&&$Grade!="Select One")
{
$result=mysql_query("SELECT * FROM programmes WHERE Programme_Id='$P_Id' AND School_Id='$S_Id' AND Year='$Year' AND Grade='$Grade'");
	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
 $Programme_Id=$row['Programme_Id'];
  $res=mysql_query("SELECT * FROM programme WHERE Programme_Id='$Programme_Id'");
  if($rowp=mysql_fetch_array($res))
  {
  $p_name=$rowp['Programme_Name'];
  }
 $Grade=$row['Grade'];
 $Required_Teachers=$row['Required_Teachers'];
$no_m_stu=$row['No_Of_Male_Students'];
$no_f_stu=$row['No_Of_Female_Students'];
$te_in_trai=$row['Teachers_In_Training'];
$tra_tea=$row['Trained_Teachers'];
$Ava_books=$row['Availability_Books'];
    $Ava_T_Gui=$row['Availability_Tguides'];
	}
 if(mysql_num_rows($result)==0)
   {
	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
    else
   {
   if($_SESSION['Access_Type']=="Administrator")
   {
    
	{
   	  $del=mysql_query("DELETE FROM programmes WHERE Programme_Id='$P_Id' AND School_Id='$S_Id' AND Year='$Year'");
	if(mysql_affected_rows()!=0)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }

  if(mysql_affected_rows()==0)
  {
 $_SESSION['error']="Sorry unable to delete this record!";
 $Programme_Id='';
 $Required_Teachers='';
  $p_name='';
    $Grade='';
$no_m_stu='';
$no_f_stu='';
$te_in_trai='';
$tra_tea='';
$Ava_books='';
    $Ava_T_Gui='';
  }
  }
   } 
if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
	$Accessed_By=$_SESSION['NIC_Number'];
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
  if($row=mysql_fetch_array($result))
   {
   $Required_Teachers=$row['Required_Teachers'];
   $Pro_Id=$row['Programme_Id'];
   $Year=$row['Year'];
     $Grade=$row['Grade'];
   $Sch_Id=$row['School_Id'];
  $no_m_stu=$row['No_Of_Male_Students'];
$no_f_stu=$row['No_Of_Female_Students'];
   $No_Tra_tea=$row['Teachers_In_Training'];
   $Tra_tea=$row['Trained_Teachers'];
   $Avai_Book=$row['Availability_Books'];
  $Avai_Tea_Gui=$row['Availability_Tguides'];
  $Last_Updated_Date=$row['Last_Updated_Date']; 
   }
   	 $sql=mysql_query("INSERT INTO deleted_programmes(Grade,Required_Teachers,Programme_Id,Year,School_Id,No_Of_Male_Students,No_Of_Female_Students,Teachers_In_Training,Trained_Teachers,Availability_Books,Availability_Tguides,Deleted_date,Accessed_By,Last_Updated_Date) VALUES('$Grade','$Required_Teachers','$Pro_Id','$Year','$Sch_Id','$no_m_stu','$no_f_stu','$No_Tra_tea','$Tra_tea','$Avai_Book','$Avai_Tea_Gui','$t_d','$Accessed_By','$Last_Updated_Date')");
if($sql){
	$del=mysql_query("DELETE FROM programmes WHERE Programme_Id='$P_Id' AND School_Id='$S_Id' AND Year='$Year' AND Grade='$Grade'");
	if($del)
  {
$_SESSION['error']="This record is successfully deleted!";
 
  }
  if(!$del)
  {
  $Required_Teachers='';
   $Programme_Id='';
  $p_name='';
   $Grade='';
$no_m_stu='';
$no_f_stu='';
$te_in_trai='';
$tra_tea='';
$Ava_books='';
    $Ava_T_Gui='';
 $_SESSION['error']="Sorry unable to delete this record!";
 mysql_query("DELETE FROM deleted_programmes WHERE Programme_Id='$Programme_Id' AND Year='$Year' AND Grade='$Grade' AND Deleted_Date='$t_d'");
 }
 }
    if(!$sql)
{
$Required_Teachers='';
 $Programme_Id='';
  $p_name='';
$no_m_stu='';
$no_f_stu='';
$te_in_trai='';
$tra_tea='';
$Ava_books='';
   $Grade='';
    $Ava_T_Gui='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
   }
   else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  $Programme_Id='';
     $Grade='';
  $Required_Teachers='';
  $p_name='';
$no_m_stu='';
$no_f_stu='';
$te_in_trai='';
$tra_tea='';
$Ava_books='';
    $Ava_T_Gui='';
  }
   }   
  else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
  $Programme_Id='';
  $Required_Teachers='';
  $p_name='';
$no_m_stu='';
$no_f_stu='';
$te_in_trai='';
   $Grade='';
$tra_tea='';
$Ava_books='';
    $Ava_T_Gui='';
  }
  }
  }
  }
  }
if($Pro_Name=="Select One"||$SchName==""||$Year=="Select One"||$Grade=="Select One")
	   {
		   $_SESSION['errorin']="Not enough inputs to delete!";
   
	   }  
   
  ?>
